var vueApp = new Vue({
  el: '#app',
  mixins: [vue2editor],
  data: function () {
    return {
    	msgId:'',
    	oper:'add',
    	mtpOptions:[],
    	msgType:'',    	
    	header:'',
    	url:'msglist.html?',
    	url2:'recvlist.html?',
    	loading: false,
    	createDate: new Date(),
    	gsbm:'',
    	cascaderdeptOpt:{  			
    		 showallevels:true,  //false 仅显示一级  true 显示多级  	
    		 changeonselect:true,//若需要允许用户选择任意一级选项，则可将change-on-select赋值为true ; false 只能选择最后一级
    		 clearable:true,//可清除
    		 filterable:true,//将filterable赋值为true即可打开搜索功能
    		 defaultProps: {
    			children : 'children',
    			label : 'label',
    			value : 'id'
    		 },
    	},    		
    	cascaderdept:{  		    
    		 depts:[],   		  
    		 deptvalue:[], //默认值通过数组的方式指定
    	},  
    	title:'',
    	editorContent:'',
    	editorHeight: 300,//编辑器内容区域高度，不设置默认是自动高度
    	fileArgs:{},
    	fdelable:true,
//    	comArgs:{},
    	activeNames: ['1','2'],
    	//comDel:true,
    	recvr:[],
    	recvrs:[],
    	uploadUrl:'../../pub/attach/uploadsingle',
    	fileNo:'',
    	data:{},
    	typeLimit:['image/jpg','image/jpeg','image/png','application/vnd.openxmlformats-officedocument.wordprocessingml.document',
    	           'application/msword','text/plain'],
    	fileFlag: false,
    	fileUploadPercent:0,
      };
  },
  mounted: function () {
	  this.init();
  },
  methods: {
	  init:function(){
		 this.msgId = tools.getQueryString("msgId");
	     this.mtpOptions.push.apply(this.mtpOptions,msg_type);
	     this.cascaderdept=tools.getDeptOpts(parseInt(userExInfo.deptCode),"0","0"); 
	     this.gsbm = userExInfo.deptCode;
	     this.oper = tools.getQueryString("oper")==""?this.oper:tools.getQueryString("oper");
		 this.setHeader();
		 this.getRecvrs(0);
		 this.oper=="read"?(this.fdelable=false):"";
		 (this.oper=="edit"||this.oper=="read")?this.getInfo():"";
		 if(this.msgId!=""){
			 this.fileArgs.fileNo = this.fileNo;
			 this.fileArgs.msgId = this.msgId;
			 this.$refs.flist.onSearch();
//			 this.comArgs.msgId = this.msgId;
//			 this.$refs.clist.onSearch();
		 }else{
			 this.getMsgId();
		 }	 
		 
	  },
	  getRecvrs:function(id){
		  var reqData = {"id" : id };
		  url="../../pub/userInfo/deptUserTreeEx";        	    	
		  data = tools.ajaxRequest(url,reqData);    
		  this.recvrs = data.tree;
		 
	  },
	  getRecvMax:function(root){
		  var that =this;
		  var num = 1;
		  if(root.children!=undefined&&root.children instanceof Array){
			  for(var i=0;i<root.children.length;i++){
				  num = num+ that.getRecvMax(root.children[i]);
			  }
		  }
		  return num;
	  },
	  setHeader:function(){
		  if(this.oper=="add"){
			  $("title").html("消息添加");
			  this.header= "消息添加";
		  }else if(this.oper=="edit"){
			  $("title").html("消息编辑");
			  this.header= "消息编辑";
		  }else if(this.oper=="read"){
			  $("title").html("消息查看");
			  this.header= "消息查看";
		  }
	  },
	  getMsgId:function(){
		  var that=this;
		  var jsonStr={
				 
		  };
		  var reqData={
			"jsonStr":JSON.stringify(jsonStr)
		  };
		  tools.ajaxRequestAsyncEx("pub/msg/getMaxId", reqData, function(data){
		   		if(data.respCode !=0){
		   			that.showError(data.respDesc);   
		   			return false;
			    }else{
			    	that.msgId = data.msg_id;
  	            }			    
		  });		  
	  },
	  getInfo:function(){
		  var that=this;
		  var jsonStr={
		   "msg_id": this.msgId
		  };
		  var reqData={
			"jsonStr":JSON.stringify(jsonStr)
		  };
		  var data = tools.ajaxRequestEx("pub/msg/messageInfo", reqData);
		  if(data.respCode !=0){
			  that.showError(data.respDesc);   
			  return false;
		  }else{
			  var info = data.list[0];
			  that.msgType = info.msg_type;
			  that.createDate = new Date(info.create_date.replace(/-/g,'/'));
			  that.gsbm = info.gsbm;
			  that.cascaderdept=tools.getDeptOpts(parseInt(that.gsbm),"0","0"); 
			  that.title = info.msg_title;
			  that.editorContent = info.msg_cont;
			  that.fileNo = info.file_no;
  	       }			      
		  // 获取recvs
		  jsonStr = {
	    	  "msgId" : this.msgId,
	    	  "operType": "sendUser",
	      };
	      reqData ={
	    	 "jsonStr": JSON.stringify(jsonStr),
	    	 "currentPageNbr" : "1",
	    	 "pageSize": this.getRecvMax(this.recvrs[0])  
	      };
	      tools.ajaxRequestAsyncEx("pub/msg/msgDetailList", reqData, function(data){
	    	  if (data.respCode == "0") {
	    	       console.log(data.list);
	    	       if(data.list.length>0){
	    	    	   data.list.forEach(function(item,idx){
	    	    		   that.recvr.push(item.recv_oper_code+"_p");
	    	    	   });
	    	       }	    	       
	    	  }else{
	    	      that.recvr= [];
	    	      that.showError(data.respDesc);   
	    	      return false;
	    	  }
	      });
		  
		  
	  },	  
	  deptChg:function(value){
	  	var arry = tools.deptchange(value,this.cascaderdept.depts);
	  	this.gsbm= arry[arry.length-1];
	  },
	  beforeUploadFile: function(file) {
	    	this.data.fileName= file.name;
	    	this.data.fileType = file.type;
	    	this.data.fileSize = file.size;
	    	if(this.fileNo!=""){
	  		    this.data.fileNo = this.fileNo;
  		    }
  		    this.data.modeName = "消息录入";
  		    this.data.folder= "/file/msg";
	        this.data.modeName="上传";
	    	
	        const isLt10M = file.size / 1024 / 1024  < 50;
	        if (this.typeLimit.indexOf(file.type) == -1) {
	        	this.showError("上传文件格式不正确");
	            return false;
	        }
	        if (!isLt10M) {
	        	this.showError("上传文件大小不能超过50MB!");
	            return false;
	        }

	  },
	  uploadFileProcess: function(event, file, fileList){
		  console.log(event);
		  var that = this;          
		  var complete = (event.loaded / event.total * 100 | 0);
          that.$showProgress(complete);
	  },
	  handleFileSuccess: function (res, file) {   
		  var that =this;
	      var fileNo = res.fileList[0].fileNo;
	      if(this.fileNo==""){
	    	  this.fileNo = fileNo;
	      }	  	      
	      this.fileArgs.fileNo = this.fileNo;
	      this.$refs.flist.refresh();	    
	      setTimeout(function(){
	    	  that.$showProgress(false);
	    	  that.$message({
	 	         showClose: true,
	 	         type: 'success',
	 	         message: "上传成功"
	 	      });
	      },2000);

	  },
	  handleFileError:function(err, file, fileList){
		  this.$showProgress(false);
	  },
	  
/*
	  uploadFileProcess: function(event, file, fileList){
		  console.log(event);
	      this.fileFlag = true;
	      this.fileUploadPercent = event.percent;
		  this.loading = true;
	  },
	  handleFileSuccess: function (res, file) {   
		  var that =this;
	      var fileNo = res.fileList[0].fileNo;
	      if(this.fileNo==""){
	    	  this.fileNo = fileNo;
	      }	        
	      this.$message({
	         showClose: true,
	         type: 'success',
	         message: "上传成功"
	      });
	      this.fileArgs.fileNo = this.fileNo;
	      this.$refs.flist.refresh();
	      this.loading = false;
	      setTimeout(function(){
	    	that.fileFlag = false;
	    	that.fileUploadPercent = 0;
	      },2000);

	  },
	  handleFileError:function(err, file, fileList){
		  this.showError(err);
		  this.loading = false;
		  this.fileFlag = false;
	      this.fileUploadPercent = 0;
	  },
	  
*/	  
	  check:function(){
		if(this.msgType==""){
			this.showError("消息类型不能为空"); 
			return false;
		}
		if(this.gsbm=="-1"){
			this.showError("归属部门不能为空"); 
			return false;
		}
		if(this.createDate==null||this.createDate==""){
			this.showError("创建时间不能为空"); 
			return false;
		}
		if(this.title.trim()==""){
			this.showError("标题不能为空"); 
			return false;
		}
		
		if(this.recvr.length==0||this.recvr.join(",").indexOf("_p")<0){
			console.log(this.recvr);
			this.showError("请选择部门下消息接收人");
			return false;
		}
		
		return true;
	  },
	  save:function(){		  
		  if(!this.check()){  
			  return false;
		  }		  
		  var that=this;
		  var jsonStr={
		     "operType": this.oper,
		     "msgId": this.msgId,
		     "createDate": util.formatTime(this.createDate,'yyyy-MM-dd'),
		     "msgType":this.msgType,
		     "msgTitle": this.title,
		     "fileNo": this.fileNo,
		     "msgCont":this.editorContent,
		     "gsbm":this.gsbm,
		     "recvs": this.recvr.join(",")
		  };
		  var reqData={
			"jsonStr":JSON.stringify(jsonStr)
		  };
		  
		  tools.ajaxRequestAsyncEx("pub/msg/messageSave", reqData, function(data){
		   		if(data.respCode !=0){
		   			that.showError(data.respDesc);   
		   			return false;
			    }else{
			    	that.$message({
			            showClose: true,
			            type: 'success',
			            message: "保存成功"
			        });  
			    	that.back();
  	            }			    
		   });		  
		  
		  
	  },
	  back:function(){
		  if(tools.getQueryString("org")==0){   // 消息管理
			  window.location.href=this.url+"msgType="+tools.getQueryString("msgType")+"&msgSts="+tools.getQueryString("msgSts")
			  +"&gsbm="+tools.getQueryString("gsbm")+"&title="+tools.getQueryString("title")+"&beginDate="+tools.getQueryString("beginDate")
			  +"&endDate="+tools.getQueryString("endDate");
		  }else{   //我的消息
			  window.location.href=this.url2+"msgType="+tools.getQueryString("msgType")+"&read_status="+tools.getQueryString("read_status")
			  +"&title="+tools.getQueryString("title")+"&beginDate="+tools.getQueryString("beginDate")
			  +"&endDate="+tools.getQueryString("endDate");
		  }	  
		  
	  },
     
      showError: function(info){
          var i = (info==undefined?"加载数据错误":info);
          this.$alert(i, '系统错误', {
               confirmButtonText: '确定'
          }); 
      },
    },
  watch:{
      
  }
});

